﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Smartec.Framework.Common;

namespace Smartec.Accounts.Camel.AMS.Model.Managers
{
    public static class MailServices
    {
        public static bool SendEmail(string fromAddress, string toAddress, List<string> bcc, string subject, string messageBody, bool isBodyHtml)
        {
            if (bcc != null && bcc.Count > 100)
            {
                bool resValue = true;
                for (int i = 0; i < bcc.Count / 100 + (bcc.Count % 100 > 0 ? 1 : 0); i++)
                {
                    resValue = resValue & SendEmail(fromAddress, toAddress, bcc.GetRange(i * 100, (bcc.Count - i * 100 > 100) ? 100 : bcc.Count - i * 100), subject, messageBody, isBodyHtml);
                }
                return resValue;
            }

            MailMessage message = new MailMessage(fromAddress, toAddress);

            if (bcc != null)
            {
                if (bcc.Count > 0)
                {
                    foreach (var item in bcc)
                    {
                        message.Bcc.Add(item);
                    }
                }
            }
            SmtpClient smtp = new SmtpClient("localhost", 25);
            string mail = ConfigurationBase.ApplicationConfiguration.GetValue("configuration/smtp/servercredintials/Email");
            string password = ConfigurationBase.ApplicationConfiguration.GetValue("configuration/smtp/servercredintials/password");
            System.Net.NetworkCredential c = new System.Net.NetworkCredential(mail, password);
            smtp.Credentials = c;
            if (ConfigurationBase.ApplicationConfiguration.GetValue("configuration/local/@IsLocal") == "true")
            {
                smtp = new SmtpClient();
                smtp.PickupDirectoryLocation = "D://Mails";
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
            }
            message.Body = messageBody;
            message.Subject = subject;
            message.IsBodyHtml = isBodyHtml;
            smtp.Send(message);
            return true;
        }
        public static bool SendEmail(string fromAddress, string subject, string messageBody, bool isBodyHtml)
        {

            string toAddress = ConfigurationBase.ApplicationConfiguration.GetValue("configuration/smtp/to");
            MailMessage message = new MailMessage(fromAddress, toAddress);

          
            SmtpClient smtp = new SmtpClient("localhost", 25);
            string mail = ConfigurationBase.ApplicationConfiguration.GetValue("configuration/smtp/servercredintials/Email");
            string password = ConfigurationBase.ApplicationConfiguration.GetValue("configuration/smtp/servercredintials/password");
           
            System.Net.NetworkCredential c = new System.Net.NetworkCredential(mail, password);
            smtp.Credentials = c;
            if (ConfigurationBase.ApplicationConfiguration.GetValue("configuration/local/@IsLocal") == "true")
            {
                smtp = new SmtpClient();
                smtp.PickupDirectoryLocation = "D://Mails";
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
            }
            message.Body = messageBody;
            message.Subject = subject;
            message.IsBodyHtml = isBodyHtml;
            smtp.Send(message);
            return true;
        }

        public static void SendBulkEmail(string fromAddress, List<string> toAddresses, string subject, string messageBody, bool isBodyHtml)
        {
            string toAddress = ConfigurationBase.ApplicationConfiguration.GetValue("configuration/smtp/to");
            SendEmail(fromAddress, toAddress, toAddresses, subject, messageBody, isBodyHtml);
        }
    }
}
